package dev.alm.openaidemo.controller;

import jakarta.servlet.http.HttpServletResponse;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

@RestController
@RequestMapping("chat")
public class ChatController {

    private final ChatModel deepSeekChatModel;

    public ChatController(ChatModel chatModel) {
        this.deepSeekChatModel = chatModel;
    }

    @RequestMapping("simple")
    public String simple() {
        String text = deepSeekChatModel.call(new Prompt("你好，你是谁？")).getResult().getOutput().getText();
        return text;
    }

    @RequestMapping("stream")
    public Flux<String> stream(HttpServletResponse response) {
        response.setCharacterEncoding("UTF-8");
        Flux<ChatResponse> stream = deepSeekChatModel.stream(new Prompt("请将一个冷笑话"));
        return stream.map(ChatResponse::getResult).map(result -> result.getOutput().getText());
    }



}
